package com.woniuxy.lrz.carlproject.common;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Aspect
@Slf4j
@Component
public class LogAspect {

    @Around("@annotation(com.woniuxy.lrz.carlproject.common.Log)")
    public Object logOutputInfo(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        MethodSignature ms = (MethodSignature) proceedingJoinPoint.getSignature();
        String operation = ms.getMethod().getAnnotation(Log.class).value();
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        String finalOp = operation.trim().length() == 0 ? "" : "[" + operation + "]";

        log.info("执行业务"+finalOp +",参数:{}", proceedingJoinPoint.getArgs());
        Object result = proceedingJoinPoint.proceed();
        stopWatch.stop();
        log.info("业务"+ finalOp +"执行完毕: {} 耗时{}ms", result,stopWatch.getTotalTimeMillis());
        return result;
    }
}
